package com.ruoyi.vote.admin.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.vote.admin.domain.vo.VotePrizeVo;
import com.ruoyi.vote.admin.domain.bo.VotePrizeBo;
import com.ruoyi.vote.admin.service.IVotePrizeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 奖品管理
 *
 * @author 筑巢
 * @date 2023-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vote/prize")
public class VotePrizeController extends BaseController {

    private final IVotePrizeService iVotePrizeService;

    /**
     * 奖品管理列表
     */
    @SaCheckPermission("vote:prize:list")
    @GetMapping("/list")
    public TableDataInfo<VotePrizeVo> list(VotePrizeBo bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("create_time");
        pageQuery.setIsAsc("desc");
        return iVotePrizeService.queryPageList(bo, pageQuery);
    }

    /**
     * 奖品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("vote:prize:query")
    @GetMapping("/{id}")
    public R<VotePrizeVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iVotePrizeService.queryById(id));
    }

    /**
     * 新增奖品
     */
    @SaCheckPermission("vote:prize:add")
    @Log(title = "奖品管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VotePrizeBo bo) {
        return toAjax(iVotePrizeService.insertByBo(bo));
    }

    /**
     * 修改奖品
     */
    @SaCheckPermission("vote:prize:edit")
    @Log(title = "奖品管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VotePrizeBo bo) {
        return toAjax(iVotePrizeService.updateByBo(bo));
    }

    /**
     * 删除奖品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("vote:prize:remove")
    @Log(title = "奖品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iVotePrizeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
